﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BaseLibrary
{
    public class SMS
    {
        private static string SURL = "http://10.72.32.119:9081/sms/Js_SMSSender.do?method=saveSMS";

        /// <summary>
        /// 通过接口发送短信
        /// </summary>
        /// <param name="targets">目标号码</param>
        /// <param name="from">来自</param>
        /// <param name="content">内容</param>
        /// <param name="systype">系统名称</param>
        /// <param name="ErrorMsg">错误日志</param>
        /// <returns></returns>



        public static Boolean SendSMS(string targets, string content)
        {
            string from="任务调度平台";
            bool flag = true;
         
            if (targets.Contains(";"))
            {
                string[] targetss = targets.Split(';');

                foreach (string tar in targetss)
                {
                    if (tar != "")
                    {
                        flag = SendSingle(tar, from, content);
                       
                    }
                }

            }

            else
            {
                flag = SendSingle(targets, from, content);
            }


            return flag;
        }

        public static Boolean SendSingle(string target, string from, string content )
        {
            bool flag = true;
           string ErrorMsg = string.Empty;

            target = target.Replace("+86", "").Replace(" ", "").Replace("-", "").Trim();

            if (target.Length != 11)
            {
                ErrorMsg += target + "手机号错误" + "<br>";
                flag = false;
            }
            if (!(target.StartsWith("130") | target.StartsWith("131") | target.StartsWith("132") | target.StartsWith("145") | target.StartsWith("155") | target.StartsWith("156") | target.StartsWith("186")))
            {

                ErrorMsg += target + "只支持联通号码" + "<br>";
                flag = false;
            }

            if (content.Length == 0)
            {
                ErrorMsg += target + "内容不能为空" + "<br>";
                flag = false;
            }


            if (flag)
            {

                string url = SURL;
                string body = content;

                content = from + ":" + content;

                string poststr = "userMobile=" + from + "&userId=null&smsFlag=0&logType=%B6%CC%D0%C5%B7%A2%CB%CD&logCo=10.72.34.135&optType=%B6%CC%D0%C5%B7%A2%CB%CD&toPhoneNumber=" + target + "&content=" + body + "&sengflag=yes";



                //if (!CloudSend(target,content))
                //{
                //    flag = false;
                //    ErrorMsg += target + "发送失败";

                //}

                if (!Postget.Post2Http(url, Encoding.GetEncoding("gb2312"), poststr).Contains("发送成功。"))
                {
                    flag = false;
                    ErrorMsg += target + "发送失败";

                }
                else
                {
                    ErrorMsg += target + "发送成功";

                   
                }
            }
            ErrorMsg += "   " + DateTime.Now.ToString() + "<br>";
            LogHelper li = new LogHelper();
            li.PlugName = "任务调度平台短信提醒";
            li.Warn(ErrorMsg);
            return flag;
        }

    }
}
